package com.yzt.plugin_imageloader;

import android.graphics.Rect;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * author : bob
 * date   : 2022/11/29
 * desc   :
 */
public class NinePatchChunk {

    public final Rect mPadding = new Rect();

    public int[] mDivX;
    public int[] mDivY;
    public int[] mColor;

    private static void readIntArray(final int[] data, final ByteBuffer buffer) {
        for (int i = 0, n = data.length; i < n; ++i) {
            data[i] = buffer.getInt();
        }
    }

    private static void checkDivCount(final int length) {
        if (length == 0 || (length & 0x01) != 0) {
            throw new RuntimeException("invalid nine-patch: " + length);
        }
    }

    public static NinePatchChunk deserialize(final byte[] data) {
        final ByteBuffer byteBuffer =
                ByteBuffer.wrap(data).order(ByteOrder.nativeOrder());

        if (byteBuffer.get() == 0) {
            return null;
        }

        final NinePatchChunk chunk = new NinePatchChunk();
        chunk.mDivX = new int[byteBuffer.get()];
        chunk.mDivY = new int[byteBuffer.get()];
        chunk.mColor = new int[byteBuffer.get()];

        checkDivCount(chunk.mDivX.length);
        checkDivCount(chunk.mDivY.length);

        byteBuffer.getInt();
        byteBuffer.getInt();

        chunk.mPadding.left = byteBuffer.getInt();
        chunk.mPadding.right = byteBuffer.getInt();
        chunk.mPadding.top = byteBuffer.getInt();
        chunk.mPadding.bottom = byteBuffer.getInt();

        byteBuffer.getInt();

        readIntArray(chunk.mDivX, byteBuffer);
        readIntArray(chunk.mDivY, byteBuffer);
        readIntArray(chunk.mColor, byteBuffer);

        return chunk;
    }
}
